package com.sqm.conntroller;

import com.sqm.entity.Comment;
import com.sqm.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能:
 * 作者: 沙琪马
 * 日期: 2024/11/27 19:02
 */
@RestController
@RequestMapping("comments")
public class CommentController {

    @Resource
    private CommentService commentService;

    @GetMapping
    public List<Comment> getComments(@RequestParam String postId) {
        System.out.println("接收到的 postId: " + postId); // 调试用
        List<Comment> comments = commentService.getCommentsByPostId(postId);
        if (comments == null || comments.isEmpty()) {
            System.out.println("未查询到相关评论");
            return new ArrayList<>(); // 返回空数组而不是 null
        }
        return comments;
    }

    @PostMapping
    public void addComment(@RequestBody Comment comment) {
        commentService.save(comment);
    }
}
